<?php
/**
 * Created by PhpStorm.
 * BuyUser: u
 * Date: 2018/11/8
 * Time: 14:06
 */
namespace common\sources\write\config;

use common\models\craftsman\Config;

class ConfigAccess implements IConfigAccess
{
    public function ChangePayType($data) {
        $company = new Config();
        $company->company = $data['company'];
        $company->qk_config_rmb = $data['qk_config_rmb'];
        $company->pageUrl = $data['pageUrl'];
        $company->bgUrl = $data['bgUrl'];
        $company->productName = $data['productName'];
        $company->payType = $data['payType'];
        $company->cert_path = $data['cert_path'];
        $company->pem_path = $data['pem_path'];
        if ($company->save()) {
            return true;
        }
        return false;
    }

    public function ChangePayType_1($data) {
        foreach ($data as $key => $value) {
            if ('company'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '公司名称';
                $company->ConfigValue = $value;
            }
            if ('qk_config_rmb'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '人民币网关账号';
                $company->ConfigValue = $value;
            }
            if ('pageUrl'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '支付结果页面地址';
                $company->ConfigValue = $value;
            }
            if ('bgUrl'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '接收支付结果的后台地址';
                $company->ConfigValue = $value;
            }
            if ('productName'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '商品名称';
                $company->ConfigValue = $value;
                $company->save();
            }
            if ('payType'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '支付方式';
                $company->ConfigValue = $value;
            }
            if ('cert_path'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '证书地址';
                $company->ConfigValue = $value;
            }
            if ('pem_path'==$key) {
                $exist = Config::findOne(['ConfigName' => $key]);
                if (!$exist) {
                    $company = new Config();
                }
                $company->ConfigName = $key;
                $company->ConfigDesc = '密钥地址';
                $company->ConfigValue = $value;
            }
            if (!$company->save()) {
                return false;
                break;
            }
        }
        return true;
    }

    public function UsePayConfig($data) {
        $config = Config::findOne(['id' => $data['id']]);
        if ($config) {
            $config->use = $data['use'];
            if ($config->save()) {
                return true;
            }
        }
        return false;
    }

    public function DelPayConfig($data) {
        $config = Config::findOne(['id' => $data['id']]);
        if ($config) {
            if ($config->delete()) {
                return true;
            }
        }
        return false;
    }
}